package com.jsmile.cloud.tradecenter.cart.model;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.goodscenter.api.req.spu.ReqSkuCheck;
import com.jsmile.cloud.goodscenter.api.res.ResSku;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqAddCart;
import com.jsmile.cloud.tradecenter.api.req.cart.ReqUpdateCart;
import com.jsmile.cloud.tradecenter.api.res.ResCart;
import com.jsmile.cloud.tradecenter.cart.repo.ShoppingCartSearchRepository;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2019年12月19日 02:56:26
 */
@ApiModel
@Data
@TableName("t_shopping_cart")
public class ShoppingCart extends Model<ShoppingCart> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    @ApiModelProperty(value = "用户id")
    private Long userId;
    @ApiModelProperty(value = "活动id")
    private Long activityId;
    @ApiModelProperty(value = "店铺id")
    private Long shopId;
    @ApiModelProperty(value = "供应商id")
    private Long supplyId;
    @ApiModelProperty(value = "sku_id")
    private Long skuId;
    @ApiModelProperty(value = "商品id")
    private Long spuId;
    @ApiModelProperty(value = "数量")
    private Integer skuNumber;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;
    @ApiModelProperty(value = "删除标记")
    @TableLogic
    private Integer delFlag;
    @ApiModelProperty(value = "加入购物车时的价格")
    private Long joinPrice;
    @ApiModelProperty(value = "租户id")
    private Long tenantId;
    @ApiModelProperty(value = "配送方式 1自提 2快递")
    private Integer deliveryType;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;

    // 更新购物车
    public static ShoppingCart updateCart(ReqUpdateCart reqUpdateCart) {
        ShoppingCart shoppingCart = SpringKit.getBean(ShoppingCartSearchRepository.class).findByUserIdAndSkuIdAndDeliveryTypeAndTenantIdAndShopId(reqUpdateCart.getCurUserId(),
            reqUpdateCart.getSkuId(), reqUpdateCart.getDeliveryType(), reqUpdateCart.getCurTenantId(), reqUpdateCart.getShopId());
        if (null == shoppingCart) {
            shoppingCart = BeanUtil.toBean(reqUpdateCart, ShoppingCart.class);
        } else {
            shoppingCart.setSkuNumber(reqUpdateCart.getSkuNumber());
        }
        shoppingCart.setUpdateTime(new Date());
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setTenantId(reqUpdateCart.getCurTenantId());
        shoppingCart.setUserId(reqUpdateCart.getCurUserId());
        return shoppingCart;
    }

    // 添加购物车
    public static ShoppingCart addCart(ReqAddCart reqAddCart) {
        ShoppingCart shoppingCart = SpringKit.getBean(ShoppingCartSearchRepository.class).findByUserIdAndSkuIdAndDeliveryTypeAndTenantIdAndShopId(reqAddCart.getCurUserId(),
            reqAddCart.getSkuId(), reqAddCart.getDeliveryType(), reqAddCart.getCurTenantId(), reqAddCart.getShopId());
        if (null == shoppingCart) {
            shoppingCart = BeanUtil.toBean(reqAddCart, ShoppingCart.class);
        } else {
            shoppingCart.setSkuNumber(shoppingCart.getSkuNumber() + reqAddCart.getSkuNumber());
        }
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setUpdateTime(new Date());
        shoppingCart.setUserId(reqAddCart.getCurUserId());
        shoppingCart.setTenantId(reqAddCart.getCurTenantId());
        return shoppingCart;
    }

    public ReqSkuCheck.SkuCheck createSkuCheck() {
        ReqSkuCheck.SkuCheck reqSkuCheck = BeanUtil.toBean(this, ReqSkuCheck.SkuCheck.class);
        return reqSkuCheck;
    }

    public ResCart.CartDetial createCartDetial(ResSku resSku) {
        ResCart.CartDetial cartDetial = BeanUtil.toBean(this, ResCart.CartDetial.class);
        cartDetial.setCartId(this.getId());
        if (null != resSku && null != resSku.getSpuInfo()) {
            cartDetial.setHighCouponPrice(resSku.getSpuInfo().getHighCouponPrice());
            cartDetial.setLowCouponPrice(resSku.getSpuInfo().getLowCouponPrice());
            cartDetial.setHighMarketPrice(resSku.getSpuInfo().getHighMarketPrice());
            cartDetial.setLowMarketPrice(resSku.getSpuInfo().getLowMarketPrice());
            cartDetial.setHighSellingPrice(resSku.getSpuInfo().getHighSellingPrice());
            cartDetial.setLowSellingPrice(resSku.getSpuInfo().getLowSellingPrice());
        }
        return cartDetial;
    }
}
